/**  定义订单相关的接口 */
const express = require('express');
const router = express.Router();
const Joi = require('joi');
const Response = require('../utils/Response.js');

// 引入mysql连接池
const pool = require('../utils/db.js');

/**
 * 通过旅社id添加订单信息接口
 * @param:
 *   详见接口文档
 * @return:
 *   {code:200, msg:'ok'}
 */
router.post('/order/add', (req, resp) => {
  let {
    otime,
    check_in_time,
    check_out_time,
    name,
    email,
    phone,
    status,
    housetype,
    people,
    price,
    hm_id,
  } = req.body; // post请求参数在req.body中

  // 表单验证
  let schema = Joi.object({
    otime: Joi.string().required(),
    check_in_time: Joi.string(),
    check_out_time: Joi.string(),
    name: Joi.string().required(),
    email: Joi.string().required(),
    phone: Joi.string().required(),
    status: Joi.string().required(),
    housetype: Joi.string().required(),
    people: Joi.string().required(),
    price: Joi.string().required(),
    hm_id: Joi.number().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }

  // 表单验证通过，执行添加操作
  let sql = `insert into order_message (otime,check_in_time,check_out_time,name,email,phone,status,housetype,people,price,hm_id) values (?,?,?,?,?,?,?,?,?,?,?)`;
  pool.query(
    sql,
    [
      otime,
      check_in_time,
      check_out_time,
      name,
      email,
      phone,
      status,
      housetype,
      people,
      price,
      hm_id,
    ],
    (error, result) => {
      if (error) {
        resp.send(Response.error(500, error));
        throw error;
      }
      // 获取当前添加订单信息的ID，并新增电话信息表
      let insertId = result.insertId;
      let sql2 = 'insert into phone (phone,o_id) values (?,?)';
      pool.query(sql2, [phone, insertId], (error2, result2) => {
        if (error2) {
          resp.send(Response.error(500, error2));
          throw error2;
        }
        resp.send(Response.ok());
      });
    }
  );
});

// -------------------

/**
 * 通过订单编号查询用户订单
 * @param:
 *   oid:
 * @return:
 *   {code:200, msg:'ok', data:{}}
 */
router.get('/order/query/id', (req, resp) => {
  let { id } = req.query;
  // 表单验证
  let schema = Joi.object({
    id: Joi.string().required(), // 必填
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }
  // 执行查询业务
  let sql = `SELECT * FROM order_message where oid=?`;
  pool.query(sql, [id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    if (result && result.length == 0) {
      // 没查到
      resp.send(Response.ok(null));
    } else {
      resp.send(Response.ok(result));
    }
  });
});

// -------------------

/**
 * 通过邮箱查询用户订单
 * @param:
 *   email:
 * @return:
 *   {code:200, msg:'ok', data:{}}
 */
router.get('/order/query/email', (req, resp) => {
  let { email } = req.query;
  // 表单验证
  let schema = Joi.object({
    email: Joi.string().required(), // 必填
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }
  // 执行查询业务
  let sql = `SELECT * FROM order_message where email=?`;
  pool.query(sql, [email], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    if (result && result.length == 0) {
      // 没查到
      resp.send(Response.ok(null));
    } else {
      resp.send(Response.ok(result));
    }
  });
});

// -----------------------------------------

/**
 * 通过电话查询用户订单及酒店信息
 * @param:
 *   phone:
 * @return:
 *   {code:200, msg:'ok', data:{}}
 */
router.get('/order/query/phone', (req, resp) => {
  let { phone } = req.query;
  // 表单验证
  let schema = Joi.object({
    phone: Joi.string().required(), // 必填
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }
  // 执行查询业务
  let sql = `SELECT om.*,hm.* FROM order_message om left join hotel_message hm on om.hm_id = hm.hmid where om.phone=?`;
  pool.query(sql, [phone], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    if (result && result.length == 0) {
      // 没查到
      resp.send(Response.ok(null));
    } else {
      resp.send(Response.ok(result));
    }
  });
});

// --------------------------------

/**
 * 通过用户id查询用户订单
 * @param:
 *   phone:
 * @return:
 *   {code:200, msg:'ok', data:{}}
 */
router.get('/order/query/id', (req, resp) => {
  let { id } = req.query;
  // 表单验证
  let schema = Joi.object({
    id: Joi.string().required(), // 必填
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }
  // 执行查询业务
  let sql = ``;
  pool.query(sql, [id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    if (result && result.length == 0) {
      // 没查到
      resp.send(Response.ok(null));
    } else {
      resp.send(Response.ok(result));
    }
  });
});

// ---------------------------------

// 将router对象导出
module.exports = router;
